// 超全局变量
global._ = require('lodash')
global.APP_PATH = process.cwd()
global.path = require('path')
global.fs = require('fs')
global.configure = require('little-man-config')
global.pager = require('../../framework/lib/pager')
global.log4js = require('../../framework/lib/log4js')
global.helper = require('../../framework/lib/helper')
global.moment = require('moment')

const apiFree = require('api-free')                             // 载入api-free 核心组件

global.M = apiFree.M
global.DB = apiFree.DB
global.dbUtil = apiFree.dbUtil

// 初始化 api-free
apiFree.init(configure.get('apifree')).then(async ()=>{
    // 供应商
    await dbUtil.createOrUpdate('dic_notice_provider', { code: 'PROVIDER_ALI_CLOUD' }, {
        code: 'PROVIDER_ALI_CLOUD',
        name: '阿里云',
    })
    await dbUtil.createOrUpdate('dic_notice_provider', { code: 'PROVIDER_JI_PUSH' }, {
        code: 'PROVIDER_JI_PUSH',
        name: '极光',
    })

    await dbUtil.createOrUpdate('dic_notice_provider', { code: 'PROVIDER_SYSTEM_IM' }, {
        code: 'PROVIDER_SYSTEM_IM',
        name: '系统通知服务',
    })

    // 下发方式
    await dbUtil.createOrUpdate('dic_notice_way', { way_code: 'PROVIDER_ALI_CLOUD_SMS' }, {
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        provider_code: 'PROVIDER_ALI_CLOUD',
        app_key: 'LTAI4FuZ293af5XZxNyj7VxQ',
        app_secret: 'qpVvBgvlVfsd6CuBAiA9NvVNfJrpen',
        callback: '',
        way_name: '短信'
    })
    await dbUtil.createOrUpdate('dic_notice_way', { way_code: 'PROVIDER_ALI_CLOUD_MNS' }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        app_key: 'LTAI4FuZ293af5XZxNyj7VxQ',
        app_secret: 'qpVvBgvlVfsd6CuBAiA9NvVNfJrpen',
        way_name: '语音电话'
    })
    await dbUtil.createOrUpdate('dic_notice_way', { way_code: 'PROVIDER_JI_PUSH_SOCKET' }, {
        provider_code: 'PROVIDER_JI_PUSH',
        way_code: 'PROVIDER_JI_PUSH_SOCKET',
        app_key: '630c9e205780075fc31e5d71',
        app_secret: 'f31d6ca494e3d65980527e2a',
        way_name: '极光推送'
    })

    await dbUtil.createOrUpdate('dic_notice_way', { way_code: 'IM_WEBSOCKET' }, {
        provider_code: 'PROVIDER_SYSTEM_IM',
        way_code: 'IM_WEBSOCKET',
        way_name: '系统websocket服务'
    })

    // 签名
    await dbUtil.createOrUpdate('dic_notice_sign', {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        sign_name: '博微物联',
    }, { provider_code: 'PROVIDER_ALI_CLOUD', way_code: 'PROVIDER_ALI_CLOUD_SMS', sign_name: '博微物联',sign_name_parse:'博微物联'})

    await dbUtil.createOrUpdate('dic_notice_sign', {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        sign_name: '055162143488',
    }, { provider_code: 'PROVIDER_ALI_CLOUD', way_code: 'PROVIDER_ALI_CLOUD_MNS', sign_name: '055162143488',sign_name_parse:'055162143488'})

    // 短信模板
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_182895048',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_182895048',
        template_code_parse:'SMS_182895048',
        title: '用户注册验证码',
        content: '验证码${code}，您正在注册成为新用户，感谢您的支持！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_182895051',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_182895051',
        template_code_parse:'SMS_182895051',
        title: '身份验证验证码',
        content: '验证码${code}，您正在进行身份验证，打死不要告诉别人哦！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_184220391',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_184220391',
        template_code_parse:'SMS_184220391',
        title: '火警短信通知3',
        content: '你好，${unit_name}有设备发生${device_status}，设备信息：${device_name}，位置：${device_location}，状态：${device_detail_status}，请及时登录企业安全画像确认火警！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_184210408',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_184210408',
        template_code_parse:'SMS_184210408',
        title: '火警短信通知2',
        content: '火警！你好，${unit_name}的${building_name}发生火灾警告，火灾确认人${confirm_user_name}，确认时间${confirm_time}，电话${confirm_mobile}，火灾设备信息：${device_info}，请登录企业安全画像查看详细信息！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_192825273',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_192825273',
        template_code_parse:'SMS_192825273',
        title: '隐患/任务逾期警告',
        content: '请尽快处理${danger_count}条逾期单位隐患，${patrol_count}条逾期巡查巡检，${fire_count}条火警处置，${leave_count}条人员离岗。'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_182668149',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_182668149',
        template_code_parse:'SMS_182668149',
        title: '火警短信通知模板',
        content: '火警!你好,${unit_name}发生火灾警告,火灾确认人${confirm_name},确认时间${confirm_time},电话${confirm_phone},火灾设备信息:${device_info}…请登录企业安全画像查看详细信息!'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_194910647',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_194910647',
        template_code_parse:'SMS_194910647',
        title: '密码重置',
        content: '您的账号：${account}，密码重置成功！新密码为：${password}。请及时登录修改密码！'
    })

    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_194920494',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_194920494',
        template_code_parse:'SMS_194920494',
        title: '开通账号',
        content: '您的管理员刚刚给您开通了企业安全画像的系统账号，账号：${account}，初始密码：${password}，请及时登录修改密码！'
    })



    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205405529',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205405529',
        template_code_parse:'SMS_205405529',
        title: '企业端，点击真警按钮时触发=0',
        content: '${unit}用户${user}在${date}触发真警，请紧急处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205441035',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205441035',
        template_code_parse:'SMS_205441035',
        title: '企业端，点击真警按钮时触发>0',
        content: '${time}期间，有${count}家单位触发真警，请紧急处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205405546',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205405546',
        template_code_parse:'SMS_205405546',
        title: '设备状态为火警时=0',
        content: '设备状态为火警时=0'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205441039',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205441039',
        template_code_parse:'SMS_205441039',
        title: '设备状态为火警时>0',
        content: '${time}期间，有${count}家单位发生设备火警，请尽快处理！'
    })

    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205400571',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205400571',
        template_code_parse:'SMS_205400571',
        title: '设备状态为异常/离线/联动运行时=0',
        content: '${unit}设备${device}在${time}发生${event}，请尽快处理！'
    })

    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205445987',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205445987',
        template_code_parse:'SMS_205445987',
        title: '设备状态为异常/离线/联动运行时>0',
        content: '${time}期间，有${count}家单位发生设备异常，请尽快处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205441027',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205441027',
        template_code_parse:'SMS_205441027',
        title: '当企业隐患/火警处置/巡查巡检/人员离岗超过逾期时长时=0',
        content: '隐患逾期${unit}在${time}造成${type}逾期，请尽快处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205436099',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205436099',
        template_code_parse:'SMS_205436099',
        title: '当企业隐患/火警处置/巡查巡检/人员离岗超过逾期时长时>0',
        content: '${time}期间，有${count}家单位发生隐患逾期，请尽快处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205390583',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205390583',
        template_code_parse:'SMS_205390583',
        title: '设备首次维修时间前一周=0',
        content: '${unit}设备${device}在${time}需返厂维修，请尽快处理！'
    })

    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'SMS_205436098',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_SMS',
        template_code: 'SMS_205436098',
        template_code_parse:'SMS_205436098',
        title: '设备首次维修时间前一周>0',
        content: '${time}期间，有${count}家单位存在设备首次维修时间即将到期，请尽快处理！'
    })

    //语音模板
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205400678',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205400678',
        template_code_parse:'TTS_205400678',
        title: '企业端，点击真警按钮时触发=0',
        content: '${unit}在${time}触发真警，请紧急处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205405632',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205405632',
        template_code_parse:'TTS_205405632',
        title: '企业端，点击真警按钮时触发>0',
        content: '在${time}期间，有${count}家单位触发真警，请紧急处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205400682',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205400682',
        template_code_parse:'TTS_205400682',
        title: '设备状态为火警时=0',
        content: '${unit}在${time}发生设备报警，请尽快处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205400683',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205400683',
        template_code_parse:'TTS_205400683',
        title: '设备状态为火警时>0',
        content: '在${time}期间，有${count}家单位发生设备火警，请尽快处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205395656',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205395656',
        template_code_parse:'TTS_205395656',
        title: '设备状态为异常/离线/联动运行时=0',
        content: '${unit}在${time}发生设备异常，请尽快处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205390662',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205390662',
        template_code_parse:'TTS_205390662',
        title: '设备状态为异常/离线/联动运行时>0',
        content: '在${time}期间，有${count}家单位发生设备异常，请尽快处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205405648',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205405648',
        template_code_parse:'TTS_205405648',
        title: '当企业隐患/火警处置/巡查巡检/人员离岗超过逾期时长时=0',
        content: '${unit}在${time}造成隐患逾期，请尽快处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205405651',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205405651',
        template_code_parse:'TTS_205405651',
        title: '当企业隐患/火警处置/巡查巡检/人员离岗超过逾期时长时>0',
        content: '在${time}期间，有${count}家单位发生隐患逾期，请尽快处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205400705',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205400705',
        template_code_parse:'TTS_205400705',
        title: '设备首次维修时间前一周=0',
        content: '${unit}有灭火器设备需要在${time}返厂维修，请尽快处理！'
    })

    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_205400709',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_205400709',
        template_code_parse:'TTS_205400709',
        title: '设备首次维修时间前一周>0',
        content: '在${time}期间，有${count}家单位存在设备首次维修时间即将到期，请尽快处理！'
    })

    // 企业端的5条语音通知
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_209815005',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_209815005',
        template_code_parse:'TTS_209815005',
        title: '企业端语音通知-火警通知',
        content: '火警通知！火警通知！${unit_name}的${building_name}发生火灾警告，请及时登录企业安全画像查看火警！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_209835097',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_209835097',
        template_code_parse:'TTS_209835097',
        title: '企业端语音通知-设备报警',
        content: '设备报警通知！设备报警通知！${unit_name}的${device_name}发生报警，请及时登录企业安全画像确认火警！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_209825108',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_209825108',
        template_code_parse:'TTS_209825108',
        title: '企业端语音通知-设备异常',
        content: '设备异常！设备异常！${unit_name}的${device_name}发生异常，请及时登录企业安全画像处理！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_209825110',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_209825110',
        template_code_parse:'TTS_209825110',
        title: '企业端语音通知-设备离线',
        content: '设备离线！设备离线！${unit_name}的${device_name}发生离线，请及时登录企业安全画像确认火警！'
    })
    await dbUtil.createOrUpdate('dic_notice_template', {
        template_code: 'TTS_209830100',
    }, {
        provider_code: 'PROVIDER_ALI_CLOUD',
        way_code: 'PROVIDER_ALI_CLOUD_MNS',
        template_code: 'TTS_209830100',
        template_code_parse:'TTS_209830100',
        title: '企业端语音通知-设备联动运行',
        content: '设备联动运行！设备联动运行！${unit_name}的${device_name}发生联动运行，请及时登录企业安全画像确认火警！'
    })


    // 通知类型
    await dbUtil.createOrUpdate('dic_notice_type', { type_code: 'NOTICE_EVENT' }, {
        type_code: 'NOTICE_EVENT',
        type_name: '事件'
    })
    await dbUtil.createOrUpdate('dic_notice_type', { type_code: 'NOTICE_PLAN' }, {
        type_code: 'NOTICE_PLAN',
        type_name: '任务'
    })
    await dbUtil.createOrUpdate('dic_notice_type', { type_code: 'NOTICE_CUSTOM' }, {
        type_code: 'NOTICE_CUSTOM',
        type_name: '主动通知'
    })

    log4js.debug('DONE !!!')
    process.exit(0);
}).catch(err => {
    log4js.error(err)
})




